// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.process.parts;

import java.beans.PropertyChangeEvent;

import org.dengues.commons.IDenguesCoreContants;
import org.dengues.core.ExceptionOperation;
import org.dengues.core.process.CompElement;
import org.dengues.designer.ui.editors.ProcessMultiEditor;
import org.dengues.designer.ui.editors.editmanager.CompNodeLabelDirectEditManager;
import org.dengues.designer.ui.process.editors.BlockEditorInput;
import org.dengues.designer.ui.process.figures.CompBlockFigure;
import org.dengues.designer.ui.process.models.CompBlock;
import org.dengues.designer.ui.process.models.CompBlockExit;
import org.dengues.designer.ui.process.policies.CompBlockDirectEditPolicy;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.tools.CellEditorLocator;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-4-13 qiang.zhang $
 * 
 */
public class CompBlockPart extends CompNodePart {

    private CompBlockFigure figure;

    private CompNodeLabelDirectEditManager directEditManager;

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
     */
    @Override
    protected IFigure createFigure() {
        figure = new CompBlockFigure(getCastModel());
        return figure;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.designer.ui.process.parts.CompNodePart#createEditPolicies()
     */
    @Override
    protected void createEditPolicies() {
        super.createEditPolicies();
        installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE, new CompBlockDirectEditPolicy());
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractEditPart#refreshVisuals()
     */
    @Override
    protected void refreshVisuals() {
        Point loc = getCastModel().getLocation();
        Dimension size = getCastModel().getSize();
        Rectangle rectangle = new Rectangle(loc, size);
        String compDisplayName = getCastModel().getCompName();
        figure.setName(compDisplayName);
        figure.setHint(getCastModel().getCompName());
        figure.setBackgroundColor(new Color(Display.getDefault(), getCastModel().getColor()));
        ((GraphicalEditPart) getParent()).setLayoutConstraint(this, getFigure(), rectangle);
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
     */
    @Override
    public void propertyChange(PropertyChangeEvent e) {
        String prop = e.getPropertyName();
        if (CompBlock.PROP_BLOCK_LOCATION.equals(prop) || CompBlock.PROP_BLOCK_SIZE.equals(prop)
                || CompBlock.PROP_BLOCK_COLOR.equals(prop) || CompBlockExit.PROP_BLOCKEXIT_LOCATION.equals(prop)
                || CompBlockExit.PROP_BLOCKEXIT_SIZE.equals(prop) || CompBlockExit.PROP_BLOCKEXIT_COLOR.equals(prop)
                || CompBlock.PROP_BLOCK_NAME.equals(prop)) {
            refreshVisuals();
        }
        super.propertyChange(e);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.designer.ui.process.parts.CompNodePart#performRequest(org.eclipse.gef.Request)
     */
    @Override
    public void performRequest(Request req) {
        CompElement castModel = getCastModel();
        if (castModel instanceof CompBlock) {
            CompBlock block = (CompBlock) castModel;
            if (REQ_OPEN.equals(req.getType())) {
                IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
                IWorkbenchPart part = activePage.getActiveEditor();
                if (part instanceof ProcessMultiEditor) {
                    BlockEditorInput input = new BlockEditorInput(block.getBlockId(), castModel.getCompName());
                    try {
                        activePage.openEditor(input, IDenguesCoreContants.PROCESS_EDITOR_ID);
                    } catch (PartInitException e) {
                        ExceptionOperation.operate(e);
                    }
                }
            } else if (RequestConstants.REQ_DIRECT_EDIT.equals(req.getType())) {
                if (directEditManager == null) {
                    directEditManager = new CompNodeLabelDirectEditManager(this, TextCellEditor.class,
                            new CompBlockLabelCellEditorLocator());
                }
                directEditManager.show();
            }
        }
        super.performRequest(req);
    }

    class CompBlockLabelCellEditorLocator implements CellEditorLocator {

        /*
         * (non-Javadoc)
         * 
         * @see org.eclipse.gef.tools.CellEditorLocator#relocate(org.eclipse.jface.viewers.CellEditor)
         */
        public void relocate(CellEditor celleditor) {
            Text text = (Text) celleditor.getControl();
            Rectangle rect = figure.getClientArea();
            figure.translateToAbsolute(rect);
            org.eclipse.swt.graphics.Rectangle trim = text.computeTrim(10, 10, -20, -20);
            rect.translate(trim.x, trim.y);
            rect.width += trim.width;
            rect.height += trim.height;
            text.setBounds(rect.x, rect.y, rect.width, rect.height);
        }

    }

}
